package 拦截器的使用.main.java.com.itheima.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
定义一个类，继承handlerInterceptor接口，声明这个类为一个拦截器
 */
//将其加入到springmvc中
@Component
public class ProjectInterceptor3 implements HandlerInterceptor {
    //重写三个方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //在拦截方法之前执行
        System.out.println("preHandle...");
        //这里为false表示阻止原来方法的运行，true表示执行原来的方法
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        //在拦截方法之后执行
        System.out.println("postHandle...");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        //在postHandle执行之后执行
        System.out.println("afterCompletion...");
    }
}
